<?php
namespace app\admin\controller;
use think\Controller;
use think\Db;
class Check extends controller
{
    public function Operator()
    {
        $groupId = session('group_id','','admin');
        $list = db('check_operator_list')->where('is_del',0)->order('id DESC')->paginate(12)->each(function($item, $key){
            $item['operator'] = db('admin')->where('id','IN',$item['operator_ids'])->value('GROUP_CONCAT(name)');
            return $item;
        });
        
        $this->assign(['list'=>$list]);
        return view();
    }
    
    public function operatorContent()
    {
        return view();
    }
    
    public function OperatorAdd()
    {
        if(request()->isAjax())
        {
            $data=input('post.');
            $data['admin_id'] = session('uid','','admin');
            $data['start_time'] = strtotime($data['start_time']);
            $data['end_time'] = strtotime($data['end_time'])+86399;
            $data['operator_ids'] = implode(',',$data['operator_ids']);
            if(input()['id'])
            {
                db('check_operator_list')->where('id',input()['id'])->update($data);
            }
            else
            {
                $data['add_time'] = time();
                db('check_operator_list')->insert($data);
            }
            return ['code'=>1,'msg'=>'操作成功','icon'=>1];
        }
        else
        {
            $operator = db('admin')->where('group_id',7)->where('status',1)->orderRaw('CONVERT(name USING gbk)')->select();
            if(input()['id'])
                $data = db('check_operator_list')->where('id',input()['id'])->find();
            else
                $data['title'] = date('Y年m').'月度运营人员考核';
                
            $data['start_time'] = $data['start_time']?date('Y-m-d',$data['start_time']):date('Y-m-01');
            $data['end_time'] = $data['end_time']?date('Y-m-d',$data['end_time']):date('Y-m-t');
            
            $data['operator_ids'] = explode(',',$data['operator_ids']);
            $this->assign(['data'=>$data,'operator'=>$operator]);
            return view();
        }
    }
    
    public function OperatorDel()
    {
        $id = input('id');
        if($id && session('group_id','','admin') == 1)
        {
            db('check_operator_list')->where('id',$id)->update(['is_del'=>1]);
            return ['code'=>1,'msg'=>'操作成功','icon'=>1];
        }
    }
    
    public function OperatorDo()
    {
        $adminId = session('uid','','admin');

        //$adminId = 14;
        $info = db('check_operator_list')->where('is_del',0)->where('id',input()['check_id'])->find();
        if(request()->isAjax())
        {
            $data = input('post.');
            $data['add_time'] = time();
            $data['admin_id'] = $adminId;
            return db('check_operator_info')->insert($data)?['code'=>1,'msg'=>'操作成功','icon'=>1]:['code'=>-1,'msg'=>'操作失败','icon'=>2];
        }
        else
        {
            $pageList = db('active_page_log')->where('to_admin_id',$adminId)->where('complete_time','>=',$info['start_time'])->where('complete_time','<=',$info['end_time'])->select();

            if($pageList)
            {
                foreach ($pageList as $k=>$v)
                {
                    if(!db('check_operator_info')->where('check_id',$info['id'])->where('admin_id',$adminId)->where('operator_id',$v['admin_id'])->value('id'))
                        $pageListN[$v['admin_id']]['list'][] = $v;
                }
                if($pageListN)
                {
                    foreach ($pageListN as $k=>$v)
                    {
                        $pageListN[$k]['name'] = db('admin')->where('id',$k)->value('name');
                    } 
                }
            }
            $this->assign(['score_1'=>[2,6,8],'score_2'=>[1,3,4,5,7,9,10],'score_3'=>[11],'pageListN'=>$pageListN ,'info'=>$info ]);
            return view();
        }

    }
    
    public function OperatorStats()
    {
        $checkId = input()['check_id'];
        if($checkId)
        {
            $info = db('check_operator_list')->where('is_del',0)->where('id',$checkId)->find();
            $list = db('admin')->where('id','IN',$info['operator_ids'])->select();
            
            foreach ($list as $k=>$v)
            {
                $list[$k]['page_count'] = 0;
                $list[$k]['result'] = db('check_operator_info')->field('CAST(AVG(score_total) AS DECIMAL(10,1)) as score_total,sum(veto_1) as veto_1,sum(veto_2) as veto_2,sum(veto_3) as veto_3 ')->where('check_id',$checkId)->where('operator_id',$v['id'])->find();
                
                $childList = db('active_page_log')->field('staff_name,to_admin_id')->where('admin_id',$v['id'])->where('complete_time','>=',$info['start_time'])->where('complete_time','<=',$info['end_time'])->group('to_admin_id')->select();
                foreach($childList as $kk=>$vv)
                {
                    $childList[$kk]['result'] = db('check_operator_info')->alias('i')->field('i.*,a.name')->join('admin a','a.id=i.admin_id')->where('i.check_id',$checkId)->where('i.operator_id',$v['id'])->where('i.admin_id',$vv['to_admin_id'])->order('id ASC')->find();
                    $childList[$kk]['result']['page_count'] = db('active_page_log')->where('admin_id',$v['id'])->where('to_admin_id',$vv['to_admin_id'])->where('complete_time','>=',$info['start_time'])->where('complete_time','<=',$info['end_time'])->count();
                    $list[$k]['page_count'] += $childList[$kk]['result']['page_count'];
                }
                $list[$k]['list'] = $childList;
                
                /*
                $list[$k]['list'] = db('check_operator_info')->alias('i')->field('i.*,a.name')->join('admin a','a.id=i.admin_id')->where('i.check_id',$checkId)->where('i.operator_id',$v['id'])->order('id ASC')->select();
                
                $otherList = db('active_page_log')->where('to_admin_id','NOT IN',db('check_operator_info')->where('check_id',$checkId)->where('operator_id',$v['id'])->value('GROUP_CONCAT(admin_id)') )->where('admin_id',$v['id'])->where('add_time','>=',$info['start_time'])->where('add_time','<=',$info['end_time'])->group('to_admin_id')->select();
                */
            }
        }
        $this->assign(['list'=>$list ,'info'=>$info ]);
        return view();
    }
    
    public function OperatorView()
    {
        $id = input()['id'];
        {
            $data = db('check_operator_info')->where('id',$id)->find();
        }
        $this->assign(['data'=>$data ]);
        return view();
    }
    
    
}